package bl.controller;

import java.util.ArrayList;

import database.factory.BookDataFactory;
import database.factory.BookDataFactoryImpl;
import database.factory.MemberDataFactory;
import database.factory.MemberDataFactoryImpl;
import database.service.BookDataService;
import database.service.MemberDataService;
import po.BookPO;
import po.MemberPO;
import vo.BLToViewResultMessage;
import vo.BookVO;
import exception.FavorMaxException;
import exception.FavorRepeatException;
import exception.MemberNotFoundException;
import exception.NotFavorException;
import bl.auxiliary.MemberFavorList;
import bl.service.FavorBLService;

public class FavorController implements FavorBLService {

	public FavorController() {
		super();
	}

	@Override
	public BLToViewResultMessage requireFavor(int memberID, int bookID)
			throws MemberNotFoundException, FavorMaxException,
			FavorRepeatException {
		MemberDataFactory memberDataFactory = new MemberDataFactoryImpl();
		MemberDataService memberDataService = memberDataFactory
				.getMemberDataService();
		memberDataService.load();
		
		MemberPO member = memberDataService.find(memberID);
		if (member == null) {
			throw new MemberNotFoundException();
		}

		MemberFavorList favorList = new MemberFavorList(member);
		boolean favor = favorList.favor(bookID);
		if (!favor) {
			return null;
		}

		memberDataService.update(member);
		memberDataService.save();

		return BLToViewResultMessage.FAVOR_SUCCESS;
	}

	@Override
	public BLToViewResultMessage cancelFavor(int memberID, int bookID)
			throws MemberNotFoundException, NotFavorException {
		MemberDataFactory memberDataFactory = new MemberDataFactoryImpl();
		MemberDataService memberDataService = memberDataFactory
				.getMemberDataService();
		memberDataService.load();
		
		MemberPO member = memberDataService.find(memberID);
		if (member == null) {
			throw new MemberNotFoundException();
		}

		MemberFavorList favorList = new MemberFavorList(member);
		boolean remove = favorList.removeFavor(bookID);
		if (!remove) {
			return null;
		}

		memberDataService.update(member);
		memberDataService.save();
		return BLToViewResultMessage.CANCEL_FAVOR_SUCCESS;
	}
	
	@Override
	public ArrayList<BookVO> viewMyFavor(int memberID) throws MemberNotFoundException {
		MemberDataFactory memberDataFactory = new MemberDataFactoryImpl();
		MemberDataService memberDataService = memberDataFactory
				.getMemberDataService();
		memberDataService.load();
		
		MemberPO member = memberDataService.find(memberID);
		if (member == null) {
			throw new MemberNotFoundException();
		}
		
		BookDataFactory bookDataFactory = new BookDataFactoryImpl();
		BookDataService bookDataService = bookDataFactory.getBookDataService();
		bookDataService.load();
		
		ArrayList<Integer> favorIDs = member.getFavorList();
		ArrayList<BookVO> list = new ArrayList<BookVO>();
		for (int id : favorIDs) {
			BookPO bookPO = bookDataService.find(id);
			if (bookPO == null) {
				continue;
			}
			BookVO bookVO = new BookVO(bookPO);
			list.add(bookVO);
		}
		
		return list;
	}

}
